//
//  XDStatusListViewModel.swift
//  weibo
//
//  Created by lucy on 2016/10/26.
//  Copyright © 2016年 com.huashengHuaSheng. All rights reserved.
//

import UIKit
import SDWebImage

class XDStatusListViewModel: NSObject {
    
    //保存微博home数据数组
    var dataArray : [XDStatusViewModel] = [XDStatusViewModel]()
    
    //请求首页数据
    func getHomeData(isPullUp:Bool,finish:@escaping (Bool)->()){
        
        var sinceId: Int64 = 0
        var maxId: Int64 = 0

        //如果是上拉加载而更多
        if isPullUp {
            maxId = dataArray.last?.statusModel?.id ?? 0
            if maxId>0{
                maxId -= 1
            }
            sinceId = 0
        }else{
            sinceId = dataArray.first?.statusModel?.id ?? 0
            maxId = 0

        }
        
        XDNetworkTool.shareTool.homeLoadData(sinceId: sinceId, maxId: maxId, success: { (response) in
            
            guard let resDict = response as? [String:Any] else{
                
                print("加载首页数据失败")
                finish(false)
                return
            }

            guard let resArr = resDict["statuses"] as? [[String:Any]] else{
                print("首页数组数据失败")
                finish(false)
                return
                
            }
            
            // yymodel使用直接转出数组
            let statusArray = NSArray.yy_modelArray(with: XDStatusModel.self, json: resArr) as! [XDStatusModel]
            
            //可变临时数组
            var tempArray:[XDStatusViewModel] = [XDStatusViewModel]()
            for model in statusArray{
                let statusViewModel = XDStatusViewModel()
                statusViewModel.statusModel = model
                tempArray.append(statusViewModel)
                
            }
            //保存数据
            //如果是上拉加载更多
            if(isPullUp){
            
                self.dataArray = tempArray + self.dataArray
            }else{
            
                self.dataArray = self.dataArray + tempArray
            }
            print(self.dataArray)
            //下载单个图片
            self.loadSingleImage(array: tempArray,finish:finish)
        }) { (error) in
            
                finish(false)
                print("请求失败",error)
        }

    }
}


extension XDStatusListViewModel{

    func loadSingleImage(array: [XDStatusViewModel],finish:@escaping (Bool)->()){
        
        //创建调度组
        let group = DispatchGroup()
        
        //遍历数组
        for statusViewModel in array{
        
            if statusViewModel.statusModel?.pic_urls?.count == 1 {
                
                let url = URL(string: statusViewModel.statusModel?.pic_urls?.first?.thumbnail_pic ?? "")!
                group.enter()
                SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (image, error, _, _, _) in
                    
                    print("原创微博下载完成")
                    group.leave()
                })
            }
            
            //如果是转发微博 && count == 1
            if statusViewModel.statusModel?.retweeted_status?.pic_urls?.count == 1 {
                let url = URL(string: statusViewModel.statusModel?.retweeted_status?.pic_urls?.first?.thumbnail_pic ?? "")!
                group.enter()
                SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (image, error, _, _, _) in
                
                print("转发微博下载完成")
                group.leave()
                })

            }

        }
        
        //调度组统一监听单张图片是否全部下载万恒
        group.notify(queue: .main){
            
            print("所有图片全部下载完成")
            finish(true)
        }
    }
}
























